﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Text;
using System.Data;

namespace MDIDemo
{
    public partial class MyComboBox : DevExpress.XtraEditors.ComboBoxEdit
    {
        string PValueMember = "";
        string PDisplayMember = "";
        DataTable _dtS = null;
        public MyComboBox()
        {
            InitializeComponent();
        }

        public MyComboBox(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }
        public string DisplayMember
        {
            get { return PDisplayMember; }
            set { PDisplayMember = value; }
        }
        public string ValueMember
        {
            get { return PValueMember; }
            set { PValueMember = value; }
        }
        public string SelectedValue
        {
            get
            {
                if (this.SelectedItem is ComboItem)
                    return ((ComboItem)this.SelectedItem).Value;
                return "";
            }
            set
            {
                bool flag = false;
                foreach (ComboItem x in this.Properties.Items)
                {
                    if (x.Value == value)
                    {
                        this.SelectedItem = x;
                        flag = true;
                        break;
                    }
                }
                if (flag == false)
                {
                    this.SelectedItem = null;
                }
            }
        }
        public DataTable DataSource
        {
            get { return _dtS; }
            set
            {
                this.Properties.Items.Clear();

                if (value != null && value.Columns.Contains(DisplayMember) && value.Columns.Contains(ValueMember))
                {
                    _dtS = value;
                    foreach (DataRow dr in _dtS.Rows)
                        this.Properties.Items.Add(new ComboItem(dr[ValueMember].ToString(), dr[DisplayMember].ToString()));
                }
                else _dtS = null;

            }

        }

    }
    public class ComboItem
    {
        string SValue = "";
        string sDisplaytext = "";
        public ComboItem(string ValueMember, string DisplayMember)
        {
            SValue = ValueMember;
            sDisplaytext = DisplayMember;
        }
        public override string ToString()
        {
            return sDisplaytext;
        }
        public string Value
        {
            get { return SValue; }
        }
    }
}
